package com.senjoeson.abilityservice;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.RecyclerView;


import com.senjoeson.abilityservice.action.ClickUserAction;
import com.senjoeson.abilityservice.action.UserSwipeAction;
import com.senjoeson.abilityservice.bean.PanelItemData;
import com.senjoeson.abilityservice.action.ActionManager;
import com.senjoeson.abilityservice.adapter.PanelItemAdapter;
import com.senjoeson.abilityservice.service.AutoAccessibilityService;

import java.util.ArrayList;
import java.util.List;

/**
 * @USER senjoeson
 * @DATE 2021/1/24 7:14 下午
 * @Desc
 **/
public class ControlPanel implements ControlAction {

    private static final String TAG = "ControlPanel";

    private WindowManager windowManager;
    private FrameLayout controlPanelParent;
    private View view;
    private final WindowManager.LayoutParams layoutParams;
    private AutoAccessibilityService autoAccessibilityService;
    private final ActionManager actionManager;


    ControlPanel(Context context) {
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        controlPanelParent = new FrameLayout(context);
        layoutParams = new WindowManager.LayoutParams();
        layoutParams.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.TOP;
        view = LayoutInflater.from(context).inflate(R.layout.panel_control_view, controlPanelParent, true);
        initView(view);
        actionManager = ActionManager.getInstance();
    }


    @SuppressLint("StaticFieldLeak")
    public static ControlPanel controlPanel;


    public static ControlPanel getInstance(Context context) {
        if (controlPanel == null) {
            synchronized (ControlPanel.class) {
                if (controlPanel == null) {
                    controlPanel = new ControlPanel(context);
                }
            }
        }
        return controlPanel;
    }


    @Override
    public void createPanel() {
        windowManager.addView(controlPanelParent, layoutParams);
    }

    @Override
    public void findText(String text) {

    }

    @Override
    public void bindService(AutoAccessibilityService autoAccessibilityService) {
        if (autoAccessibilityService != null) {
            this.autoAccessibilityService = autoAccessibilityService;
            actionManager.init(autoAccessibilityService);
        }
    }

    private void initView(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        PanelItemAdapter panelItemAdapter = new PanelItemAdapter();
        panelItemAdapter.setList(getAllAction());
        recyclerView.setAdapter(panelItemAdapter);
    }


    public List<PanelItemData> getAllAction() {
        ArrayList<PanelItemData> panelItemDataList = new ArrayList<>();
        panelItemDataList.add(new PanelItemData("电源键", () -> {
            actionManager.getSysAction().powerClick();
            return null;
        }));
        panelItemDataList.add(new PanelItemData("返回键", () -> {
            actionManager.getSysAction().backClick();
            return null;
        }));
        panelItemDataList.add(new PanelItemData("最近任务键", () -> {
            actionManager.getSysAction().backClick();
            return null;
        }));
        panelItemDataList.add(new PanelItemData("截图", () -> {
            actionManager.getSysAction().screenShotClick();
            return null;
        }));
        panelItemDataList.add(new PanelItemData("锁屏", () -> {
            actionManager.getSysAction().lockScreen();
            return null;
        }));
        panelItemDataList.add(new PanelItemData("home键", () -> {
            actionManager.getSysAction().goHomeClick();
            return null;
        }));
        panelItemDataList.add(new PanelItemData("点击测试", () -> {
            ((ClickUserAction) actionManager.getUserAction()).ifFoundTextAndClick("图库");
            return null;
        }));

        panelItemDataList.add(new PanelItemData("测试左滑", () -> {
            ((ClickUserAction) actionManager.getUserAction()).swipeLeft();
            return null;
        }));
        return panelItemDataList;
    }


}
